package com.bishe.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.pojo.UserBill;
import com.bishe.pojo.query.UserBillQuery;
import com.bishe.pojo.response.Result;
import com.bishe.service.UserBillService;
import com.bishe.mapper.UserBillMapper;
import org.springframework.stereotype.Service;

/**
 * @author wwz
 * @description 针对表【user_bill】的数据库操作Service实现
 * @createDate 2024-02-26 23:54:14
 */
@Service
public class UserBillServiceImpl extends ServiceImpl<UserBillMapper, UserBill>
        implements UserBillService {

    @Override
    public Result<?> addOrUpdate(UserBill userBill) {
        if (ObjectUtil.isEmpty(userBill)) {
            return Result.error("账单信息不能为空！");
        }

        UserBill bill = getById(userBill.getId());
        if (ObjectUtil.isNotEmpty(bill)) {
            updateById(userBill);
            return Result.ok("更新成功！",userBill);
        }
        userBill.setId(IdUtil.fastSimpleUUID());
        save(userBill);
        return Result.ok("保存成功！",userBill);

    }

    @Override
    public IPage<UserBill> toPage(UserBillQuery query) {
        return page(query.toPage(), Wrappers.lambdaQuery(UserBill.class)
                .eq(UserBill::getUid, StpUtil.getLoginIdAsString())
                .eq(StrUtil.isNotEmpty(query.getType()),UserBill::getType,query.getType())
                .between(StrUtil.isAllNotBlank(query.getStartTime(), query.getEndTime()),UserBill::getBillDate,query.getStartTime(), query.getEndTime())
                .orderByAsc(UserBill::getBillDate)
        );
    }
}




